package cn.com.checne.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.checne.model.domain.NewsInfo;

@Repository
public class ChecNewsDao {
	protected static final Logger log = Logger.getLogger(ChecNewsDao.class);

	/*public List<NewsInfo> getYestodayNewsFromCNList(String yestoday) {
		List<NewsInfo> list = new ArrayList<NewsInfo>();
		NewsInfo news = null;
		try {
			Connection conn = SqlServerConnect.getChecCNConnection();
			String sql = "SELECT title,brief,content,addtime from catalog_001_item where nodeid = '10' and addtime >= ? and addtime <= ? order by addtime desc ;";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, yestoday + " 00:00:00");
			pstmt.setString(2, yestoday + " 23:59:59");
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				news = new NewsInfo();
				news.setTitle(rs.getString("title"));
				news.setSummary(rs.getString("brief"));
				news.setContent(rs.getString("content"));
				news.setPublishDate(rs.getDate("addtime"));

				list.add(news);
			}
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return list;
	}*/
    public List<NewsInfo> getYestodayNewsFromCNList(String yestoday) {
		List<NewsInfo> list = new ArrayList<NewsInfo>();
		NewsInfo news = null;
		try {
			Connection conn = SqlServerConnect.getChecCNConnection();
			String sql = "SELECT post_title,post_summary,post_content,post_publish_time from tbl_post where post_category_type = 'NEWS' and post_publish_time >= ? and post_publish_time <= ? order by post_publish_time desc ;";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, yestoday + " 00:00:00");
			pstmt.setString(2, yestoday + " 23:59:59");
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				news = new NewsInfo();
				news.setTitle(rs.getString("post_title"));
				news.setSummary(rs.getString("post_summary"));
				news.setContent(rs.getString("post_content"));
				news.setPublishDate(rs.getTimestamp("post_publish_time"));

				list.add(news);
			}
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return list;
	}

	public List<NewsInfo> getYestodayNewsFromENList(String yestoday) {
		List<NewsInfo> list = new ArrayList<NewsInfo>();
		NewsInfo news = null;
		try {
			Connection conn = SqlServerConnect.getChecENConnection();
			String sql = "SELECT news_title,news_newsmeta,news_content,news_publishtime from tbl_news_i where  news_publishtime >= ? and news_publishtime <= ? order by news_publishtime desc ";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, yestoday + " 00:00:00");
			pstmt.setString(2, yestoday + " 23:59:59");
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				news = new NewsInfo();
				news.setTitle(rs.getString("news_title"));
				news.setSummary(rs.getString("news_newsmeta"));
				news.setContent(rs.getString("news_content"));
				news.setPublishDate(rs.getDate("news_publishtime"));
				list.add(news);
			}
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return list;
	}
}